﻿var editor;

var flag = false;

function validate() {
    return $("#frm").valid();
};

function get() {
    $.post("CMS/Handlers/CMS_NoticeHandler.ashx?type=get", { NoticeID: $("#NoticeID").val() }, function (data) {
        $('#frm').fill(data);
        editor.html(data.Content);
    }, 'json');
}

$(function () {

    editor = KindEditor.create('#Content', {
        uploadJson: 'Controls/KindEditor/asp.net/upload_json.ashx',
        fileManagerJson: 'Controls/KindEditor/asp.net/file_manager_json.ashx',
        allowFileManager: true
    });

    if ($("#NoticeID").val().length > 0)
        get();

    $("#btnSubmit").click(function () {
        editor.sync();
        var content = document.getElementById('Content');
        content.value = content.value.replace(/</g, "[").replace(/>/g, "]");
        $("#frm").ajaxSubmit({
            dataType: 'json',
            beforeSubmit: function (arr, $form, options) {
                if (flag) {
                    return false;
                }
                else {
                    flag = true;
                    return true;
                }
                
            },
            success: function (data) {
                if (data.isSuccess) {
                    loadPage("CMS/CMS_NoticeList.aspx");
                }
                else {
                    jalert(data.message);
                }
                flag = false;
            }
        });
    })
    
    $("#btnCancel").click(function () {
        loadPage("CMS/CMS_NoticeList.aspx");
    });
});
